// pages/woshoucangde/woshoucangde.js
Page({
  data: {
    favorites: [],
    num: 0
  },

  onLoad: function() {
    this.loadFavorites();
  },

  // 加载收藏列表
  loadFavorites: function() {
    const userFavorites = wx.getStorageSync('userFavorites') || [];
    const favorites = userFavorites.map(id => {
      // 假数据
      const fakeProducts = {
        '001': {id: '001',imageUrl: '/images/shangpin1.jpg',title: '回收纯手工穿戴甲款式都要 高价回收 没有大瑕疵都...',price: 6 },
        '002': {id: '002',imageUrl: '/images/shangpin2.jpg',title: '50根黑色皮筋女扎头',price: 5 },
        '003':{id: '003',imageUrl: '/images/shangpin3.jpg',title: '十三余家的遇见神鹿m码铃铛坏了 其他都...',price: 30},
        '004':{id: '004',imageUrl: '/images/shangpin4.jpg',title: '【学生党】本人自用的...',price: 6},
        '005':{id: '005',imageUrl: '/images/shangpin5.jpg', title: '洗衣机全新未拆宿舍适用特价...',price: 240},
        '006':{ id: '006',imageUrl: '/images/shangpin6.jpg.png', title: '艾米兔正版包包 在tb压的货TB压货现清...',price: 10,},
      };
      return fakeProducts[id] || { id: id, title: '商品信息丢失', imageUrl: '/images/zuopinlogo.jpg', price: 0 };
    });
    this.setData({
      favorites: favorites,
      num: favorites.length
    });
  },

  // 取消收藏
  cancelFavorite: function(e) {
    const productId = e.currentTarget.dataset.id;
    const userFavorites = wx.getStorageSync('userFavorites') || [];
    const index = userFavorites.indexOf(productId);
    if (index > -1) {
      userFavorites.splice(index, 1);
      wx.setStorageSync('userFavorites', userFavorites);
      this.loadFavorites(); // 重新加载收藏列表
      wx.showToast({
        title: '取消收藏成功',
        icon: 'success'
      });
    }
  },

  // 跳转到商品详情页
  goToDetail: function(e) {
    const productId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/shangpinxiangqing/shangpinxiangqing?productId=${productId}`
    });
  }
});